##########################################
## Copyright 2012-2013 Ceruti Francesco & contributors
##
## This file is part of LiSP (Linux Show Player).
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget
import math


class Pitch(PreferencesWidget):

    NAME = 'Pitch - Speed'

    def __init__(self, size, parent=None):
        super(Pitch, self).__init__(size, parent)

        self.groupBox = QtGui.QGroupBox(self)
        self.groupBox.setGeometry(0, 0, self.width(), 120)

        self.gridLayout = QtGui.QGridLayout(self.groupBox)

        self.speedSlider = QtGui.QSlider(self.groupBox)
        self.speedSlider.setMinimum(1)
        self.speedSlider.setMaximum(1000)
        self.speedSlider.setPageStep(1)
        self.speedSlider.setProperty("value", 100)
        self.speedSlider.setOrientation(QtCore.Qt.Horizontal)
        self.speedSlider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.speedSlider.setTickInterval(10)
        self.gridLayout.addWidget(self.speedSlider, 0, 0)

        self.speedLabel = QtGui.QLabel(self.groupBox)
        self.speedLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.speedLabel, 0, 1)

        self.pitchSlider = QtGui.QSlider(self.groupBox)
        self.pitchSlider.setMinimum(-12)
        self.pitchSlider.setMaximum(12)
        self.pitchSlider.setPageStep(1)
        self.pitchSlider.setProperty("value", 0)
        self.pitchSlider.setOrientation(QtCore.Qt.Horizontal)
        self.pitchSlider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.pitchSlider.setTickInterval(1)
        self.gridLayout.addWidget(self.pitchSlider, 1, 0)

        self.pitchLabel = QtGui.QLabel(self.groupBox)
        self.pitchLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.pitchLabel, 1, 1)

        self.gridLayout.setColumnStretch(0, 3)
        self.gridLayout.setColumnStretch(1, 1)

        self.speedSlider.valueChanged.connect(self.speedChanged)
        self.pitchSlider.valueChanged.connect(self.pitchChanged)

        self.retranslateUi()

    def retranslateUi(self):
        self.groupBox.setTitle("Speed - Pitch")
        self.speedLabel.setText("speed x1.0")
        self.pitchLabel.setText("+0 semitones")

    def enableCheck(self, enable):
        self.groupBox.setCheckable(enable)
        self.groupBox.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(not (self.groupBox.isCheckable() and not self.groupBox.isChecked())):
            conf['pitch'] = {'tempo': self.speedSlider.value() / 100, 'pitch': math.pow(2, self.pitchSlider.value() / 12)}
        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('pitch' in conf):
                if(type(conf['pitch']) == type({})):
                    if('tempo' in conf['pitch']):
                        self.speedSlider.setValue(conf['pitch']['tempo'] * 100)
                    else:
                        self.speedSlider.setEnabled(False)
                    if('pitch' in conf['pitch']):
                        self.pitchSlider.setValue(round(12 * math.log(conf['pitch']['pitch'], 2)))
                    else:
                        self.pitchSlider.setEnabled(False)
                else:
                    self.setEnabled(False)
            else:
                self.setEnabled(False)

    def speedChanged(self, value):
        self.speedLabel.setText('speed x' + str(value / 100.0))

    def pitchChanged(self, value):
        if value < 0:
            self.pitchLabel.setText(str(value) + ' semitones')
        else:
            self.pitchLabel.setText('+' + str(value) + ' semitones')
